package com.japp.manager.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.japp.base.BaseAction;
import com.japp.manager.bean.CustomerBean;
import com.japp.manager.bean.CustomerInfoBean;
import com.japp.manager.service.CustomerService;
import com.japp.sys.service.SelectService;
/**
 * 客户管理
 * @author Jion
 *
 */
@Scope("prototype")
@Controller
public class CustomerAction extends BaseAction<CustomerBean> {

	private static final long serialVersionUID = -1323063707456411899L;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private SelectService selectService;

	private CustomerBean c = new CustomerBean();
	private CustomerInfoBean ci = new CustomerInfoBean();
	private List<CustomerInfoBean> list_ci = new ArrayList<CustomerInfoBean>();
	private List<CustomerInfoBean> list_ci1 = new ArrayList<CustomerInfoBean>();
	private Map<String, String> map = null;
	private String[] del_key = null;
	
	/**
	 * 分页查询
	 * @return
	 */
	public String queryCustomer(){
		String power = session.getAttribute("u_power").toString();
		String dpid = session.getAttribute("userltd").toString();
		String dpcode = session.getAttribute("userltdcode").toString();
		String usercode = session.getAttribute("u_code").toString();
		//部门
		if("2".equals(power)){
			c.setC_suoshu_dpcode(dpcode);
		//业务员
		}else if("3".equals(power)){
			c.setC_suoshu_ucode(usercode);
		}
		map = selectService.getSelMapByString("2,3,4");
		page = customerService.getPage(page, c);
		return SUCCESS;
	}
	/**
	 * 预新增查询
	 * @return
	 */
	public String yinserCustomer(){
		c.setC_type_status("1");
		map = customerService.yinserCustomer();
		return "ctDetil";
	}
	/**
	 * 新增操作
	 * @return
	 */
	public String insertCustomer(){
		int rel = customerService.iinsertCustomerAndInfo(c,list_ci);
		if(rel>0){
			result = "客户“"+c.getC_name()+"”新增成功!";
			c = new CustomerBean();
		}else {
			result = "客户“"+c.getC_name()+"”新增失败!";
			return "ctDetil";
		}
		return SUCCESS;
	}
	/**
	 * 预更新
	 * @return
	 */
	public String yupdateCustomer(){
		map = customerService.yinserCustomer();
		c = customerService.findCustomerById(c.getC_id());
		list_ci = customerService.findCustomerInfoByCId(c.getC_id());
		return "ctDetil";
	}
	/**
	 * 更新
	 * @return
	 */
	public String updateCustomer(){
		int rel = customerService.updateCustomer(c,list_ci,list_ci1);
		if(rel>0){
			result="客户“"+c.getC_name()+"”更新成功!";
		}else{
			result="客户“"+c.getC_name()+"”更新失败!";
		}
		 yupdateCustomer();
		return "ctDetil";
	}
	
	/**
	 * 查看
	 * @return
	 */
	public String viewCustomer(){
		return "ctView";
	}
	
	public String deleteCustomer(){
		int rel = customerService.deleteCustomerById(keys);
		if(rel>0){
			result = "删除成功!";
		}else{
			result = "删除失败!";
		}
		queryCustomer();
		return SUCCESS;
	}
	
	
	
	
	
	public List<CustomerInfoBean> getList_ci1() {
		return list_ci1;
	}
	public void setList_ci1(List<CustomerInfoBean> list_ci1) {
		this.list_ci1 = list_ci1;
	}
	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	
	public String[] getDel_key() {
		return del_key;
	}

	public void setDel_key(String[] del_key) {
		this.del_key = del_key;
	}

	public CustomerBean getC() {
		return c;
	}
	public void setC(CustomerBean c) {
		this.c = c;
	}
	public CustomerInfoBean getCi() {
		return ci;
	}
	public void setCi(CustomerInfoBean ci) {
		this.ci = ci;
	}
	public List<CustomerInfoBean> getList_ci() {
		return list_ci;
	}
	public void setList_ci(List<CustomerInfoBean> list_ci) {
		this.list_ci = list_ci;
	}
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	public void setSelectService(SelectService selectService) {
		this.selectService = selectService;
	}
	
}
